<?php
/**
 * Created by PhpStorm.
 * User: yangyou
 * Date: 2018/12/3 0003
 * Time: 10:10
 * 施工案例
 */

namespace app\index\controller;

use app\index\model\Designer;
use app\index\model\Information;
use think\Config;
use app\index\model\Cases;
use app\index\model\Drawing;
use app\index\model\InformationKeyword;

class Construct extends Base
{
    /**
     * Author:yang you
     * Time:2018-11-15 10:00:43
     * Describe:施工案例列表
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function caselist($key = "")
    {
        $keyword = input("keyword/s");
        $structure = input("structure/d");//结构Id
        $house = input("house/d");//户型结构ID
        $order = input("order/d");//排序ID
        $p = input("p/d");//分页参数
        $structureName ='';//结构名称
        $layct = '';//楼层id
        $layctPY = '';//楼层拼音
        $layctName = '';//楼层名称
        $style_id = '';//风格id
        $stylePY = '';//风格拼音
        $styleName = '';//风格名称
        $orders = '';//人气，面积，价格，最新，排序
        $baseUrl = request()->baseUrl();
        $paraUrl = $this->paraUrl($structure,$house);

        //人气，面积，价格，最新，排序参数后缀URL,start
        $orderParaUrl = (substr(request()->url(),strlen(request()->baseUrl())));
        if(!empty($p)){
            //判断是直接分页还是搜索后再分页
            $domain = strstr($orderParaUrl, '&');
            if($domain != false){
                $orderParaUrl = substr($orderParaUrl,0,strpos($orderParaUrl,'&p'));
            }else{
                $orderParaUrl = substr($orderParaUrl,0,strpos($orderParaUrl,'p')-1);
            }
        }
        //人气，面积，价格，最新，排序参数后缀URL,end

        //别墅层数
        $drawingfloor = config("numberBuildings");
        foreach ($drawingfloor as $k=>$val){
            //获取后缀参数URL
            if(!empty($structure)){
                if(!empty($house)){
                    $drawingfloor[$k]['config_url'] = "?structure=".$structure."&house=".$house;
                }else{
                    $drawingfloor[$k]['config_url'] = "?structure=".$structure;
                }
            }else{
                if($house){
                    $drawingfloor[$k]['config_url'] = "?house=".$house;
                }else{
                    $drawingfloor[$k]['config_url'] = "";
                }
            }
        }
        $Drawing = new Drawing();
        $config = new \app\index\model\Config();
        $informationKeyword = new InformationKeyword();
        $information = new Information();
        //别墅风格
        $drawingStyle = $config->getConfigTypeListPY("DrawingStyle");
        foreach ($drawingStyle as $k=>$val){
            //获取后缀参数URL
            if(!empty($structure)){
                if(!empty($house)){
                    $drawingStyle[$k]['config_url'] = "?structure=".$structure."&house=".$house;
                }else{
                    $drawingStyle[$k]['config_url'] = "?structure=".$structure;
                }
            }else{
                if($house){
                    $drawingStyle[$k]['config_url'] = "?house=".$house;
                }else{
                    $drawingStyle[$k]['config_url'] = "";
                }
            }
        }

        //别墅风格
        $drawingStyle = $config->getConfigTypeListPY("DrawingStyle");
        foreach ($drawingStyle as $k=>$val){
            //获取后缀参数URL
            if(!empty($structure)){
                if(!empty($house)){
                    $drawingStyle[$k]['config_url'] = "?structure=".$structure."&house=".$house;
                }else{
                    $drawingStyle[$k]['config_url'] = "?structure=".$structure;
                }
            }else{
                if($house){
                    $drawingStyle[$k]['config_url'] = "?house=".$house;
                }else{
                    $drawingStyle[$k]['config_url'] = "";
                }
            }
        }

        if(!empty($key)){
            $is = true;
            //找楼层
            foreach ($drawingfloor as $val){
                $str = stristr($key,$val["screen_one"]);
                if($str !== false){
                    $is = false;
                    //层数搜索存在
                    if($key==$val["screen_one"]){
                        //单个搜索
                        $layct = $val['config_id'];
                        $layctPY = $val["screen_one"];
                        $layctName = $val["config_name"];
                        break;
                    }else{
                        //楼层和风格联合搜索
                        $layct = $val['config_id'];
                        $layctPY = $val["screen_one"];
                        $layctName = $val["config_name"];
                        $style = str_replace($val["screen_one"],"",$str);
                        foreach ($drawingStyle as $v){
                            if($v['py']==$style){
                                $style_id = $v['config_id'];
                                $stylePY = $style;
                                $styleName = $v['config_name'];
                                break;
                            }
                        }
                    }
                }
            }
            //找不到楼层
            if($is){
                foreach ($drawingStyle as $sk=>$SVal) {//遍历风格
                    if ($SVal["py"] == $key) {
                        $style_id = $SVal['config_id'];
                        $stylePY = $key;
                        $styleName = $SVal['config_name'];
                        break;
                    }
                }

            }
        }

        //人气，面积，价格，最新，排序
        if(!empty($order)){
            $orderInfo = config("drawingOrder");
            foreach ($orderInfo as $val){
                if($order == $val["order_id"]){
                    $orders["type"] = $val["type"];
                    $orders["type_order"] = $val["type_order"];
                }

            }
        }

        //结构形式
        $structureStyle = $config->configType("DrawingFrameStructure");
        foreach ($structureStyle as $key=>$val){
            if($val["config_id"] == $structure){
                $structureName = $val['config_name'];
            }
            if(!empty($house)){
                $structureStyle[$key]['config_url'] = "?structure=".$val["config_id"]."&house=".$house;
            }else{
                $structureStyle[$key]['config_url'] = "?structure=".$val["config_id"];
            }
        }

        $config_info = $config->drawingList();//配置参数的ID

        Config::load(APP_PATH.'admin/config.php');//加载上传配置文件
        $ImgApiData = config("ImgApiData");
        $cases = new Cases();
        $caseList = $cases->caseList($style_id,$layct,$is_top='',$classification_id="13",$structure,$orders);
        $caseListCount = $cases->caseListCount($style_id,$layct,$is_top='',$classification_id="13",$structure,$orders);
        $addressList = $this->http_request($ImgApiData["domain"].'/'.$ImgApiData["ApiList"]["allAddressList"]);
        $addressList = json_decode($addressList,true)['data'];
        //右边案例推荐
        $rightCase = $cases->leftCaseList($limit=2);
        //热门标签
        $hotTag =  $information->randKeywordInfo();
        //右侧12篇文章
        $informationList =  $information->informationList($classification_id = "",$limit = "18",$left="6",$center="12");
        $seo_info = $this->selectKey($layctName,$styleName,$structureName);
        return view('construct/caselist',[
            'caseList' => $caseList,
            'page' => $caseList->render(),
            'caseListCount' => $caseListCount,
            'structureStyle' => $structureStyle,
            'drawingStyle' => $drawingStyle,
            'drawingfloor' => $drawingfloor,
            'style_id' => $style_id,
            'layct' => $layct,
            'layctName' => $layctName,
            'stylePY' => $stylePY,
            'styleName' => $styleName,
            'layctPY' => $layctPY,
            'baseUrl' => $baseUrl,
            'structure' => $structure,
            'structureName' => $structureName,
            'house' => $house,
            'paraUrl' => $paraUrl,
            'orderParaUrl' => $orderParaUrl,
            'order' => $order,
            'config_info' => $config_info,
            'rightCase' => $rightCase,
            'hotTag' => $hotTag,
            'informationList' => $informationList,
            'addressList' => $addressList,
            'seo_info' => $seo_info
        ]);
    }

    //SEO标题描述户型结构匹配
    function selectKey($layctName='',$styleName='',$structureName=''){
        if(empty($layctName)&&empty($styleName)&&empty($structureName)) {
            $seo_Info = '';
            return $seo_Info;
        }
        $seo_Info['seo_title'] = $styleName.$structureName.$layctName."别墅设计案例-别墅工场";
        $seo_Info['seo_keyword'] = $styleName.$structureName.$layctName."别墅设计案例";
        $seo_Info['seo_description'] = "这里有多款来自全国各地的真实".$styleName.$structureName.$layctName."别墅设计案例，让您可以提前看到别墅设计图纸建出来是什么效果，作为自己建房的参考！";
        return $seo_Info;
    }

    /**
     * Author:yang you
     * Time:2018-11-15 10:00:43
     * Describe:施工案例详情
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function caseDetail()
    {
        $case_id = input("id/d");
        $cases = new Cases();
        $designer = new Designer();
        $information = new Information();
        $caseInfo = $cases->caseInfo($case_id);
        if(empty($caseInfo)){
            abort(404,'页面不存在');
        }
        //跟新阅读量
        $cases->setFieldValue('case_id',$case_id,'reading_number',$caseInfo['reading_number']+1);
        //展示的图集
        $drawing_atlas = json_decode($caseInfo["atlas"],true);
        //设计师随机获取
        $designerInfo = $designer->randInfo();
        //底部案例推荐
        $bottomCase = $cases->leftCaseList($limit="6",$classification_id="13",$caseInfo["style"]);
        //右侧8篇文章
        $informationList =  $information->informationList($classification_id = "",$limit = "8");

        return view('construct/caseDetail',[
            'caseInfo' => $caseInfo,
            'designerInfo' => $designerInfo,
            'bottomCase' => $bottomCase,
            'informationList' => $informationList,
            'drawing_atlas' => $drawing_atlas
        ]);
    }

}